package com.yzg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yzg.exceptionhandler.ServiceException;
import com.yzg.mapper.ProductFullReductionMapper;
import com.yzg.mapper.ProductLadderMapper;
import com.yzg.pojo.ProductFullReduction;
import com.yzg.pojo.ProductLadder;
import com.yzg.pojo.vo.DiscountVO;
import com.yzg.pojo.vo.ProductSearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yzg.mapper.ProductMapper;
import com.yzg.pojo.Product;
import com.yzg.service.ProductService;
import org.springframework.util.StringUtils;

/**
* @Author: decade
* @Description: ${description}
* @Date: 2021/1/13 20:26
**/
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService{
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductLadderMapper productLadderMapper;
    @Autowired
    private ProductFullReductionMapper productFullReductionMapper;
    @Override
    public Map<String, Object> getProductList(Integer page, Integer pageSize, ProductSearch productSearch) {
        IPage<Product> pageParam = new Page<>(page,pageSize);
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        if(productSearch!=null){
            if(productSearch.getKeyword()!=null&&!"".equals(productSearch.getKeyword())){
                wrapper.like("name", productSearch.getKeyword());
            }else if(productSearch.getProductCategoryId()!=null){
                wrapper.eq("product_category_id", productSearch.getProductCategoryId());
            }else if(productSearch.getBrandId()!=null){
                wrapper.eq("brand_id", productSearch.getBrandId());
            }else if(productSearch.getPublishStatus()!=null){
                wrapper.eq("publish_status", productSearch.getPublishStatus());
            }else if(productSearch.getVerifyStatus()!=null){
                wrapper.eq("verify_status", productSearch.getVerifyStatus());
            }
        }
        productMapper.selectPage(pageParam, wrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("list", pageParam.getRecords());
        map.put("total", pageParam.getTotal());
        return map;
    }

    //根据商品id获取优惠信息
    @Override
    public DiscountVO getDiscount(Long id) {
        //根据商品id获取商品信息
        Product product = productMapper.selectById(id);
        //根据商品id获取对应的阶梯价格列表
        QueryWrapper<ProductLadder> ladderWrapper = new QueryWrapper<>();
        ladderWrapper.eq("product_id", id);
        List<ProductLadder> ladderList =  productLadderMapper.selectList(ladderWrapper);
        //根据商品id获取对应的满减列表
        QueryWrapper<ProductFullReduction> fullWrapper = new QueryWrapper<>();
        fullWrapper.eq("product_id", id);
        List<ProductFullReduction> fullList = productFullReductionMapper.selectList(fullWrapper);
        //封装数据
        DiscountVO discountVO = new DiscountVO();
        discountVO.setId(id);
        discountVO.setPromotionType(product.getPromotionType());
        discountVO.setPromotionEndTime(product.getPromotionEndTime());
        discountVO.setPromotionStartTime(product.getPromotionStartTime());
        discountVO.setPromotionPrice(product.getPromotionPrice());
        if(fullList!=null&&fullList.size()>0){
            discountVO.setProductFullReductionList(fullList);
        }
        if(ladderList!=null&&ladderList.size()>0){
            discountVO.setProductLadderList(ladderList);
        }
        return discountVO;
    }

    //根据商品id修改优惠信息
    @Override
    public int updateDiscount(DiscountVO discountVO) {
        int row = 0;
        Product product = new Product();
        switch (discountVO.getPromotionType()){
            //0->没有促销使用原价
            case 0 :
                break;
            //1->使用促销价
            case 1 :
                ////修改商品促销时间
                product.setPromotionStartTime(discountVO.getPromotionStartTime());
                product.setPromotionEndTime(discountVO.getPromotionEndTime());
                product.setPromotionPrice(discountVO.getPromotionPrice());
                break;
            //2->使用阶梯价格
            case 2 : row = ladder(discountVO);
                break;
            //3->使用满减价格
            case 3 : row = fullReduction(discountVO);
                break;
            default: throw new ServiceException(201,"参数不合法");
        }
        //修改商品表促销类型
        product.setId(discountVO.getId());
        product.setPromotionType(discountVO.getPromotionType());
       row = productMapper.updateById(product);
        return row;
    }

    //商品删除
    @Override
    public int deleteProductById(Long id) {
        //先查询所有阶梯价格，再删除
        QueryWrapper<ProductLadder> ladderWrapper = new QueryWrapper<>();
        ladderWrapper.eq("product_id", id);
        //查询
        List<Long> ladderIds = new ArrayList<>();
        List<ProductLadder> ladderList = productLadderMapper.selectList(ladderWrapper);
        ladderList.forEach(ladder ->{
            ladderIds.add(ladder.getId());
        });
          //删除
        if(ladderIds!=null&&ladderIds.size()>0)
        productLadderMapper.deleteBatchIds(ladderIds);
        //先查询所有商品满减，再删除
        QueryWrapper<ProductFullReduction> fullWrapper = new QueryWrapper<>();
        ladderWrapper.eq("product_id", id);
        List<ProductFullReduction> fullReductionList = productFullReductionMapper.selectList(fullWrapper);
        List<Long> fullIds = new ArrayList<>();
        fullReductionList.forEach(full ->{
            fullIds.add(full.getId());
        });
        //删除
        if(fullIds!=null&&fullIds.size()>0){
            productFullReductionMapper.deleteBatchIds(fullIds);
        }
        //最后删除商品
        Product product = new Product();
        product.setId(id);
        product.setDeleteStatus(1);
        return productMapper.updateById(product);
    }

    //促销类型=2
    public int ladder(DiscountVO discountVO){
        int row = 0;
        //先删除再添加
        UpdateWrapper<ProductLadder> wrapper = new UpdateWrapper<>();
        wrapper.eq("product_id", discountVO.getId());
        productLadderMapper.delete(wrapper);
        //添加
        for(ProductLadder productLadder :  discountVO.getProductLadderList()){
            row = productLadderMapper.insert(productLadder);
        }
        return row;
    }

    //促销类型=3
    public int fullReduction(DiscountVO discountVO){
        int row = 0 ;
        //先删除再添加
        UpdateWrapper<ProductFullReduction> wrapper = new UpdateWrapper<>();
        wrapper.eq("product_id", discountVO.getId());
        productFullReductionMapper.delete(wrapper);
        //添加
        for(ProductFullReduction productFullReduction :  discountVO.getProductFullReductionList()){
            row = productFullReductionMapper.insert(productFullReduction);
        }
        return row;
    }
}
